import { defaultApi } from '../../../infra/api/http/apiFactory'

export default class MediaChooser {
  static chooseImage (count = 1) {
    return new Promise((resolve, reject) => {
      uni.chooseImage({
        count, //默认9
        sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], //从相册选择
        success: (res) => {
          resolve(res.tempFilePaths)
        },
        fail: () => {
          reject()
        }
      })
    })
  }

  static chooseImageAndUpload ({
    url,
    count = 1,
    formData = {},
    name = 'file',
    header = {}
  }) {
    return this.chooseImage(count).then(pics => {
      return Promise.all(pics.map(filePath =>
        defaultApi.uploadFile(url, { filePath, formData, name, header }))
      )
    })
  }
}